<template>
  <div>
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" label-width="68px" size="mini">
      <el-row :gutter="20">
        <el-col :span="4">
          <el-form-item label="项目区域" prop="projectRegion">
            <el-select v-model="queryParams.projectRegion" clearable placeholder="请选择"
                       @keyup.enter.native="handleQuery">
              <el-option v-for="dict in dict.type.cld_project_region" :key="dict.value" :label="dict.label"
                         :value="parseInt(dict.value)"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="项目名称" prop="projectName">
            <el-input v-model="queryParams.projectName" clearable placeholder="请输入"
                      @keyup.enter.native="handleQuery"/>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="时间" prop="month">
            <el-date-picker v-model="dateRange" end-placeholder="结束日期" range-separator="-"
                            start-placeholder="开始日期"
                            type="monthrange" value-format="yyyy-MM-dd"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label-width="10px">
            <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-hasPermi="['cld:confirm:confirmIssuance']" :disabled="multiple" plain size="mini" type="primary"
                   @click="handleConfirmIssuanceOpen">确认发放
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="CldPayDetailsList" @selection-change="handleSelectionChange" border>
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column align="center" label="项目区域" prop="projectRegion">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cld_project_region" :value="scope.row.projectRegion"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="项目名称" prop="projectName"/>
      <el-table-column align="center" label="基本工资" prop="basicSalary">
        <template slot-scope="scope">
          {{ toMoney(scope.row.basicSalary) }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="绩效津贴" prop="basedSalary">
        <template slot-scope="scope">
          {{ toMoney(scope.row.basedSalary) }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="福利津贴" prop="welfareSalary">
        <template slot-scope="scope">
          {{ toMoney(scope.row.welfareSalary) }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="加班工资" prop="overtimeSalary">
        <template slot-scope="scope">
          {{ toMoney(scope.row.overtimeSalary) }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="奖惩金额" prop="awardSalary">
        <template slot-scope="scope">
          {{ toMoney(scope.row.awardSalary - scope.row.fineSalary) }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="保险费用" prop="insuranceSalary">
        <template slot-scope="scope">
          {{ toMoney(scope.row.insuranceSalary) }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="工服费用" prop="clothesSalary">
        <template slot-scope="scope">
          {{ toMoney(scope.row.clothesSalary) }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="工作计划" prop="workSalary">
        <template slot-scope="scope">
          {{ toMoney(scope.row.workSalary) }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="应发薪酬" prop="inSalary">
        <template slot-scope="scope">
          {{ toMoney(scope.row.basicSalary + scope.row.basedSalary) }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="实发薪酬" prop="sendSalary">
        <template slot-scope="scope">
          {{ calculateSalary(scope.row) }}
        </template>
      </el-table-column>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button v-hasPermi="['cld:CldPayDetails:query']" size="mini" type="text"
                     @click="handleDetails(scope.row)">查看明细
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :limit.sync="queryParams.pageSize" :page.sync="queryParams.pageNum" :total="total"
                @pagination="getList"/>

    <!-- 确认发放 -->
    <el-dialog :close-on-click-modal="false" :visible.sync="isConfirmIssuanceOpen" append-to-body title="薪酬发放"
               width="400px">
      <el-form :model="form" :rules="rules" label-width="80px" size="mini">
        <el-form-item label="选择月份" prop="months">
          <el-date-picker
            v-model="form.months"
            placeholder="选择月"
            type="month"
            value-format="yyyy-MM">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isConfirmIssuanceOpen = false">取 消</el-button>
        <el-button type="primary" @click="handleConfirmIssuance">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {confirmIssuance, getCumulativeSalaryList} from "@/api/cld/CldPayDetails";

export default {
  name: "CldPayDetails",
  dicts: ['cld_project_region'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 时间区间
      dateRange: [],
      // 薪酬管理表格数据
      CldPayDetailsList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectRegion: null,
        projectName: null,
      },
      form: {},
      isConfirmIssuanceOpen: false
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询薪酬管理列表 */
    getList() {
      this.loading = true;
      getCumulativeSalaryList(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.CldPayDetailsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.projectId)
      this.multiple = !selection.length;
    },
    // 打开薪酬发放页面
    handleConfirmIssuanceOpen() {
      this.form = {};
      this.form.projectIds = this.ids.join(',');
      this.isConfirmIssuanceOpen = true;
    },
    /** 确认发放 */
    handleConfirmIssuance() {
      if (!this.form.months) {
        this.$message.error('请选择发放月份！');
        return;
      }
      this.$confirm(`是否确认发放[${this.form.months}]的薪酬?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.form.year = this.parseTime(this.form.months, '{y}');
        this.form.month = this.parseTime(this.form.months, '{m}');
        confirmIssuance(this.form).then(res => {
          this.$modal.msgSuccess("操作成功");
        })
      }).catch(() => {
      });
    },
    // 详情查看
    handleDetails(row) {
      this.$router.push("/CldPayDetails/cumulativeSalary/projectDetails/" + row.projectId);
    },
    /** 导出按钮操作 */
    handleExport() {
      // this.download('cld/CldPayDetails/export', {
      //   ...this.queryParams
      // }, `CldPayDetails_${new Date().getTime()}.xlsx`)
    },
    // 计算实发工资
    calculateSalary(row) {
      let money = row.inSalary + row.welfareSalary + row.overtimeSalary + (row.awardSalary + row.fineSalary) + row.insuranceSalary + row.clothesSalary + row.trainSalary + row.recruitmentSalary;
      return this.toMoney(money);
    },
  }
};
</script>
<style scoped>
.el-range-editor.el-input__inner {
  width: 100%;
}

.el-select {
  width: 100%;
}
</style>
